package cmd;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import model.Settings;
import util.PlayerUtilities;
import cmd.commands.Command;
import cmd.commands.DayScheduleCommand;
import cmd.commands.DeleteContainersCommand;
import cmd.commands.DeleteContentsCommand;
import cmd.commands.DeleteFeatureIDCommand;
import cmd.commands.DeleteInventoryDefinitionCommand;
import cmd.commands.DeletePlaylistCommand;
import cmd.commands.DeleteRecordsCommand;
import cmd.commands.MemorySizeCommand;
import cmd.commands.RemoveInventoryItemCommand;
import cmd.commands.ResetInventoryCommand;
import cmd.commands.ResetInventoryDefinitionCommand;
import cmd.commands.ResetPlayerCommand;
import cmd.commands.RestartComputerCommand;
import cmd.commands.RestartPlayerCommand;
import cmd.commands.RestartTeamViewerCommand;
import cmd.commands.SetVolumeCommand;
import cmd.commands.TimeScheduleCommand;
import cmd.commands.UpgradePlayerCommand;

public class RemoteCommandExecutor {
	private Queue<Command>					commandQueue;

	private static RemoteCommandExecutor	_instance;

	public static RemoteCommandExecutor getInstance() {
		if (_instance == null) {
			_instance = new RemoteCommandExecutor();
		}
		return _instance;
	}

	private RemoteCommandExecutor() {
		this.commandQueue = new LinkedList<Command>();
	}

	public void addCommand(Command cmd) {
		Command actualCommand = convertCommand(cmd);
		if(actualCommand!=null) {
			this.addCommand(cmd);
		}
	}
	public Command convertCommand(Command cmd) {
		Command actualCommand = null;
		switch (cmd.getCommand()) {
			case RESTART_PLAYER:
				actualCommand = new RestartPlayerCommand(cmd);
				break;
			case DELETE_CONTAINER:
				actualCommand = new DeleteContainersCommand(cmd);
				break;
			case DELETE_CONTENT:
				actualCommand = new DeleteContentsCommand(cmd);
				break;
			case DELETE_RECORD:
				actualCommand = new DeleteRecordsCommand(cmd);
				break;
			case DELETE_PLAYLIST:
				actualCommand = new DeletePlaylistCommand(cmd);
				break;
			case RESET_PLAYER:
				actualCommand = new ResetPlayerCommand(cmd);
				break;
			case SET_VOLUME:
				actualCommand = new SetVolumeCommand(cmd);
				break;
			case UPGRADE_PLAYER:
				actualCommand = new UpgradePlayerCommand(cmd);
				break;
			case RESET_INVENTORY:
				actualCommand = new ResetInventoryCommand(cmd);
				break;
			case REMOVE_INVENTORY:
				actualCommand = new RemoveInventoryItemCommand(cmd);
				break;
			case RESTART_TEAMVIEWER:
				actualCommand = new RestartTeamViewerCommand(cmd);
				break;
			case DELETE_INVENTORYDEFINATION:
				actualCommand = new DeleteInventoryDefinitionCommand(cmd);
				break;
			case REBOOT_MACHINE:
				actualCommand = new RestartComputerCommand(cmd);
				break;
			case REMOVE_ALL_INVENTORY:
				actualCommand = new RemoveInventoryItemCommand(cmd);
				break;
			case RESET_INVENTORYDEFINITION:
				actualCommand = new ResetInventoryDefinitionCommand(cmd);
				break;
			case FEATURE_DEF_DEL:
				actualCommand = new DeleteFeatureIDCommand(cmd);
				break;
			case MEMORY_SIZE:
				actualCommand = new MemorySizeCommand(cmd);
				break;
			case RESTART_SCHEDULE_DAY:
				actualCommand = new DayScheduleCommand(cmd);
				break;
			case RESTART_SCHEDULE_TIME:
				actualCommand = new TimeScheduleCommand(cmd);
				break;
			default:
				// returns a command that does nothing
				return new Command(null, null);
		}
		return actualCommand;
	}

	public boolean executeNext() {
		if (!this.commandQueue.isEmpty()) {
			Command c = this.commandQueue.poll();
			System.out.println("executing command: " + c);
			c.execute();
		}
		return false;
	}

	public void executeCommands() {
		File commandsFile = new File(Settings.COMMANDS_XML);

		// read the commands
		if (commandsFile.exists()) {
			//System.out.println("EXECUTE COMMANDS FILE EXISTS");
			Object obj = PlayerUtilities.getObjectFromXMLFile(Settings.COMMANDS_XML);
			if (obj != null && obj instanceof List) {
				// execute the commands
				@SuppressWarnings("unchecked")
				List<Command> commands = (List<Command>) obj;
				while(!commands.isEmpty()) {
					Command actualCommand = this.convertCommand(commands.remove(0));
					System.out.println("executing command: " + actualCommand);
					actualCommand.execute();
				}
				new File(Settings.COMMANDS_XML).delete();
			}
		}
	}
	
	public static void main(String[] args) {
		new RemoteCommandExecutor().executeCommands();
	}
}
